<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="queryParams.serviceName" placeholder="服务名" class="filter-item search-item" />

      <el-button class="filter-item" type="primary" plain @click="search">
        {{ $t('table.search') }}
      </el-button>
      <el-button class="filter-item" type="warning" plain @click="reset">
        {{ $t('table.reset') }}
      </el-button>
      <!-- 添加 -->
      <el-button class="filter-item" type="success" plain @click="add">
        {{ $t('table.add') }}
      </el-button>

    </div>

    <el-table
      ref="table"
      :key="tableKey"
      v-loading="loading"
      :data="list"
      border
      fit
      style="width: 100%;"
    >
      <el-table-column label="数据库名" prop="tableSchema" :show-overflow-tooltip="true" align="center" min-width="80px">
        <template slot-scope="scope">
          <span>{{ scope.row.tableSchema }}</span>
        </template>
      </el-table-column>
      <el-table-column label="作者" :show-overflow-tooltip="true" align="center" min-width="40px">
        <template slot-scope="scope">
          <span>{{ scope.row.author }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务名" :show-overflow-tooltip="true" align="center" min-width="80px">
        <template slot-scope="scope">
          <span>{{ scope.row.serviceName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="基础包名" :show-overflow-tooltip="true" align="center" min-width="80px">
        <template slot-scope="scope">
          <span>{{ scope.row.basePackage }}</span>
        </template>
      </el-table-column>
      <el-table-column label="entity路径" :show-overflow-tooltip="true" align="center" min-width="50px">
        <template slot-scope="scope">
          <span>{{ scope.row.entityPackage }}</span>
        </template>
      </el-table-column>
      <el-table-column label="mapper路径" :show-overflow-tooltip="true" align="center" min-width="50px">
        <template slot-scope="scope">
          <span>{{ scope.row.mapperPackage }}</span>
        </template>
      </el-table-column>
      <el-table-column label="xml路径" :show-overflow-tooltip="true" align="center" min-width="50px">
        <template slot-scope="scope">
          <span>{{ scope.row.mapperXmlPackage }}</span>
        </template>
      </el-table-column>
      <el-table-column label="service路径" :show-overflow-tooltip="true" align="center" min-width="50px">
        <template slot-scope="scope">
          <span>{{ scope.row.servicePackage }}</span>
        </template>
      </el-table-column>
      <el-table-column label="controller路径" :show-overflow-tooltip="true" align="center" min-width="50px">
        <template slot-scope="scope">
          <span>{{ scope.row.controllerPackage }}</span>
        </template>
      </el-table-column>

      <el-table-column label="去除表前缀" class-name="status-col" min-width="60px">
        <template slot-scope="{row}">
          <el-tag>
            {{ row.isTrim === 1 ? "是" : "否" }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="表前缀" :show-overflow-tooltip="true" align="center" min-width="50px">
        <template slot-scope="scope">
          <span>{{ scope.row.trimValue }}</span>
        </template>
      </el-table-column>
      <!-- 操作 -->
      <el-table-column :label="$t('table.operation')" align="center" min-width="80px" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <i v-hasPermission="['gen:config:view']" class="el-icon-view table-operation" style="color: #87d068;" @click="view(row)" />
          <i v-hasPermission="['gen:config:update']" class="el-icon-setting table-operation" style="color: #2db7f5;" @click="edit(row)" />
          <el-link v-has-no-permission="['gen:config:view','gen:config:update']" class="no-perm">
            {{ $t('tips.noPermission') }}
          </el-link>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="pagination.num" :limit.sync="pagination.size" @pagination="fetch" />
    <gen-config-edit
      ref="edit"
      :dialog-visible="dialog.isVisible"
      :title="dialog.title"
      @success="editSuccess"
      @close="editClose"
    />
    <!-- 查看代码生成配置 -->
    <GenConfigView
      ref="view"
      :dialog-visible="genConfigViewVisible"
      @close="viewClose"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import GenConfigEdit from './Edit'
import GenConfigView from './View'

export default {
  name: 'GenConfigManage',
  components: { Pagination, GenConfigEdit, GenConfigView },
  data() {
    return {
      dialog: {
        isVisible: false,
        title: ''
      },
      genConfigViewVisible: false,
      tableKey: 0,
      loading: false,
      list: null,
      total: 0,
      queryParams: {},
      pagination: {
        size: 10,
        num: 1
      }
    }
  },
  mounted() {
    this.fetch()
  },
  methods: {
    viewClose() {
      this.genConfigViewVisible = false
    },
    editClose() {
      this.dialog.isVisible = false
    },
    editSuccess() {
      this.search()
    },
    search() {
      this.fetch({
        ...this.queryParams,
        ...this.sort
      })
    },
    reset() {
      this.queryParams = {}
      this.sort = {}
      this.search()
    },
    add() {
      this.dialog.title = this.$t('common.add')
      this.dialog.isVisible = true
    },
    view(row) {
      this.$refs.view.setGenconfig(row)
      this.genConfigViewVisible = true
    },
    edit(row) {
      this.$refs.edit.setGenconfig(row)
      this.dialog.title = this.$t('common.edit')
      this.dialog.isVisible = true
    },
    fetch(params = {}) {
      params.pageSize = this.pagination.size
      params.pageNum = this.pagination.num

      this.loading = true
      this.$get('system/generatorConfig/page', {
        ...params
      }).then((r) => {
        const data = r.data.data
        this.total = data.total
        this.list = data.rows
        this.loading = false
      })
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
